/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.util;

/*******************************************************************************
 * Convenience class and constant definitions for the Net/FSE web interface.
 * Also contains constants used in the GUI. All methods and data types in this
 * class must be compatible with GWT.
 * 
 */
public class GUIUtil {

    /***************************************************************************
     * Constant used to represent searches run using multiple IPs as source IPs.
     */
    public static final int IP_SEARCH_AS_SRC = 1;

    /***************************************************************************
     * Constant used to represent searches run using multiple IPs as destination
     * IPs.
     */
    public static final int IP_SEARCH_AS_DST = 2;

    /***************************************************************************
     * Constant used to represent searches run using multiple IPs as source and
     * destination IPs.
     */
    public static final int IP_SEARCH_AS_BOTH = 3;

    /***************************************************************************
     * Constant used to represent searches run using multiple IPs.
     */
    public static final int QUERY_TYPE_IPS = 1;

    /***************************************************************************
     * Constant used to represent searches run using specific search criteria
     * (source IP, destination IP, destination port).
     */
    public static final int QUERY_TYPE_SEARCH = 2;

    /***************************************************************************
     * Constant used to represent searches using an equality predicate (as
     * opposed to a range search).
     */
    public static final int PREDICATE_EQUALS = 1;

    /***************************************************************************
     * Constant used to represent searches using a range predicate (as opposed
     * to a equality search).
     */
    public static final int PREDICATE_RANGE = 2;

    /***************************************************************************
     * Empty constructor was required for old versions of GWT.
     */
    public GUIUtil() {
    }

    /***************************************************************************
     * Given an IP address stored as a 32-bit integer, converts to dot quad.
     * Identical to same method in nfse.NetFSEUtil. Duplicate code due to
     * limitations of GWT.
     * 
     * @param ip
     *            The IP address as a 32-bit integer
     * @return The IP address as a dot quad
     */
    public static String convertLongToIP(long ip) {
        String ipStr = Long.toHexString(ip);
        while (ipStr.length() < 8)
            ipStr = "0" + ipStr;
        return convertHexToIP(ipStr);
    }

    /***************************************************************************
     * Given an IP address in hex notation (e.g. 01020304), converts it into dot
     * quad notation (e.g. 1.2.3.4). Identical to same method in
     * nfse.NetFSEUtil. Duplicate code due to limitations of GWT.
     * 
     * @param hex
     *            The IP address in hex notation
     * @return The IP address in dot quad notation
     */
    public static String convertHexToIP(String hex) {
        StringBuffer temp = new StringBuffer();
        temp.append(Integer.parseInt(hex.substring(0, 2), 16));
        if (hex.length() == 2) {
            temp.append(".0.0.0");
            return temp.toString();
        }
        temp.append(".");
        temp.append(Integer.parseInt(hex.substring(2, 4), 16));
        if (hex.length() == 4) {
            temp.append(".0.0");
            return temp.toString();
        }
        temp.append(".");
        temp.append(Integer.parseInt(hex.substring(4, 6), 16));
        if (hex.length() == 6) {
            temp.append(".0");
            return temp.toString();
        }
        temp.append(".");
        temp.append(Integer.parseInt(hex.substring(6), 16));
        return temp.toString();
    }

}